import MTExternalContent
from pymt import *

class MTVideoContent(MTExternalContent.MTExternalContent):
    name = 'picture'
    def __init__(self, filename,listPages,kinetics, **kwargs):
        super(MTVideoContent, self).__init__(**kwargs)
        self.listPages=listPages
        self._filename=filename
        print self._filename
        self.video = VideoBase(filename=self._filename)
        # self.width = (self.video.width / float(self.video.height)) * self.height
        self.kinetics=kinetics
        self.hold=False
        self.in_move_inside=False
        self.in_zone=False	
        self.in_move_outside=False
        self.zone=None
		

	
    def on_touch_move(self, touch):	
        if not self.collide_point(*touch.pos):
			return False	
        if(touch.is_held and (touch.xmot == 0 and touch.ymot == 0)):
			self.hold=True
			self.center=(touch.x,touch.y)			
        if(self.hold==True):				
			for page in self.listPages:
				media_zones=page._get_media_zones()
				for zone in media_zones:
					pos_zone=self.get_position_zone(zone,page)
					if(self.in_zone==False and (self.center[0]> pos_zone[0] and self.center[0]< pos_zone[0]+zone.width\
					and self.center[1]> pos_zone[1] and self.center[1]< pos_zone[1]+zone.height)):
						print "go inside"
						self.in_move_inside=True
						self.zone=zone
						return MTExternalContent.MTExternalContent.on_touch_move(self, touch)						
					if(self.in_zone==True):
						print "go outside"
						self.kinetics.add_widget(self)
						zone.remove_widget(self)						
						self.pos=(pos_zone[0]+(self.width/2),pos_zone[1]+(self.height/2))
						self.in_move_outside=True
						return False
					else:
						print "move"					
						self.in_move_inside=False	
						self.in_move_outside=False
			return MTExternalContent.MTExternalContent.on_touch_move(self, touch)
	
	
    def on_touch_up(self, touch):
        if not self.collide_point(*touch.pos):
			return False
        else:
		if (self.in_move_inside==True):
			print "inside"
			self.in_zone=True
			self.in_move_inside=False
			self.size=self.zone.size
			self.pos=(self.zone.width/2,self.zone.height/2)
			self.zone.add_widget(self)
			self.kinetics.remove_widget(self)
		if (self.in_move_outside ==True):
			print "outside"
			self.in_zone=False
			self.in_move_outside=False

		self.hold=False
		return MTExternalContent.MTExternalContent.on_touch_up(self, touch)
	
    def get_position_zone(self, zone, page):
		x=(zone.x-(zone.width/2))+(page.x-(page.width/2))
		y=(zone.y-(zone.height/2))+(page.y-(page.height/2))
		pos=(x,y)
		return pos

    def draw(self):
        self.size = self.video.size
        super(MTVideoContent, self).draw()
	
	
